<template>
  <div class="goods-info">
    <!-- 商品展示图 -->
    <div class="">
      <swiper :carouselItems="goodsDetail.album"></swiper>
    </div>   
    <!-- 商品购买区域 -->
    <div class="">
      <div class="">{{ goodsDetail.name }}</div>
      <div class="">
        <div class="price">
          定价：
          <span>¥{{ goodsDetail.price }}</span>
        </div>
        <div v-if="goodsDetail.num">
          <span>
            购买数量
            <number
              @onCountChanged="onCountChanged"
              :max="goodsDetail.num"
              initcount="1"
              :goodsid="goodsDetail.id"
            ></number>
          </span>
          <p class="go-buy">            
            <mt-button type="primary" size="small" @click="onClickAddShopcart"
              >加入购物车</mt-button
            >
          </p>
        </div>
        <div v-else>该商品暂时无货</div>
      </div>
    </div>
    <!-- 商品参数区域 -->

    <h5 class="">商品参数</h5>

    <div>
      <p>商品卖点：{{ goodsDetail.sell_point }}</p>
      <p>库存情况：{{ goodsDetail.num }}件</p>
      <p>上架时间：{{ goodsDetail.create_time }}</p>
    </div>

    <h5>商品详情</h5>
    <div class="good-desc">
      <div class="content" v-html="goodsDetail.content"></div>
    </div>
  </div>
</template>

<script>
import Swiper from "@/components/Swiper.vue";
import Number from '@/components/Number.vue';
import {HTTPGetGoodsDetail} from "@/api/goods.js"
import {HTTPAddCart} from "@/api/cart.js"
export default {
  data() {
    return {
      goodsDetail: {}, // 获取到的商品信息
      count: 1, // 购买的商品数量，默认是1      
      ballFlag: false, // 控制小球的显示和隐藏
    };
  },
  props: ["id"],  //商品编号
  created() {
    this.getGoodsDetail();
  },
  methods: {
    async getGoodsDetail() {      
      try {
        let result = await HTTPGetGoodsDetail({ id: this.id })        
        if(result.code == 1){          
          this.goodsDetail = result.data;          
        }else{
          throw result.msg;
        }
      } catch (error) {
        this.$messagebox("错误",error)
        this.$router.go(-1);
      }
    },//===================================================
   
    onCountChanged(goodsInfo) { //goodsinfo = { id: this.id, count: count }
      this.count = goodsInfo.count;
    },//===================================================

    async onClickAddShopcart() {      
      let params={
        goods_id: this.goodsDetail.id,
        price:this.goodsDetail.price,
        count: this.count
      };      

      try {
        let result = await HTTPAddCart(params);
        if(result.code == 1){          
          this.$toast(result.msg);  
        }else if(result.code == 2){
          this.$router.push({ name: "login" });
        }
        else{
          throw result.msg;
        }
      } catch (error) {
        this.$messagebox("错误",error)        
      }
    }, //=============================================  

  },
  components: {
    Swiper,   
    Number,
  },
};
</script>

<style lang="scss" scoped>
.goods-info {
  background: #f1f1ff;
  overflow: hidden;
  .price {
    span {
      color: red;
      font-size: 14px;
      font-weight: bold;
    }
  }
  .go-buy {
    display: flex;
    margin: 10px;
    justify-content: center;
    button {
      margin: 2px;
      width: 100%;      
    }
  }
  .good-desc {
    background: rgb(234, 236, 241);
    padding: 5px;
    .content {
      font-size: 14px;
      line-height: 28px;
      img {
        width: 100%;
      }
    }
  }

  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    position: absolute;
    background: red;
    z-index: 99;
    left: 132px;
    top: 390px;
    transform: translate(93px, 230px);
  }
}
</style>
